*********************************************************
*** 			 EXTRAPOLATION EXERCISE           	  ***
*** How much money is saved overall?                  ***
*** Extrapolate the marginal treatment effect onto    ***
*** all hospitals to estimate savings from program.   ***
*** Last edited: 8/31/22         		   	    	  ***
*********************************************************

cap mkdir "${OutputPath}/Extrapolation"
log using "${CodePath}/log/extrapolation_$S_DATE.log", text replace
preserve
	clear all
	input fyear coeff_log_tot_pmt coeff_log_compcost coeff_demand_amt coeff_log_n_claims coeff_log_mcare_in_out_pmt 
	2011 -0.0102	0.0154	314115.4	-0.0115  	-.0120009
	2012 -0.0177	0.0068	179550.2	-0.0192 	-.0210063
	2013 -0.028	0.0034	133512	-0.0191	-.0329294
	2014 -0.0216	0.0054	-5194.304	-0.0113	-.0243037
	2015 -0.0285	-0.0014	53990.35	-0.0193 	-.0316035
	2016 -0.0285	0	0	-0.0193 	-.0316035
	2017 -0.0285	0	0	-0.0193 	-.0316035
	2018 -0.0285	0	0	-0.0193 	-.0316035
	end
	tempfile coeffs
	save `coeffs', replace
restore

use pn fyear tot_pmt_amt audit_in2011 DemandAmt_3y n_claims log_real_gcost_admin_net RACregion_audit_in2011 using "${DataPath}/derived/hospyear_0716_jk.dta", clear
keep if !missing(RACregion_audit_in2011)
merge m:1 fyear using `coeffs', keep(1 3) nogen
sort pn fyear

gen real_admin_cost = exp(log_real_gcost_admin_net)
gen lawsuit = 0.32
gen DemandAmt_3y_lawsuit = DemandAmt_3y * lawsuit

// calculate actual changes relative to 2010
	foreach var of varlist tot_pmt_amt n_claims real_admin_cost{
		gen `var'_2010_t = `var' if fyear == 2010
		bys pn: egen `var'_2010 = min(`var'_2010_t)

		drop *_t

		gen actchg_`var'_from2010 =  `var' - `var'_2010
			*label var actchg_`var' "actual change in hospital-year spending from 2011"
	}

// calculate predicted changes by multiplying 2011 audit rate * coefficient for each year
	gen predchg_tot_pmt_amt_from2010 = coeff_log_tot_pmt * tot_pmt_amt_2010
		label var predchg_tot_pmt_amt_from2010 "predicted change in Medicare inpatient revenue, relative to 2010"
	gen predchg_n_claims_from2010 = coeff_log_n_claims * n_claims_2010
		label var predchg_n_claims_from2010 "predicted change in Medicare inpatient claims, relative to 2010"
	gen predchg_real_admin_cost_from2010 = coeff_log_compcost * real_admin_cost_2010
		label var predchg_real_admin_cost_from2010 "predicted change in admin costs, relative to 2010"

	// demand calculations are all assuming lawsuit
	gen pred_demand_amt = coeff_demand_amt * audit_in2011 * lawsuit
		label var pred_demand_amt "predicted demands, relative to 2010"

	// get percentiles to winsorize by
	sum actchg_tot_pmt_amt_from2010 if fyear >= 2011, detail
	local actchg_p1 = r(p1)
	local actchg_p5 = r(p5)
	local actchg_p10 = r(p10)

	local actchg_p99 = r(p99)
	local actchg_p95 = r(p95)
	local actchg_p90 = r(p90)

	sum predchg_tot_pmt_amt_from2010 if fyear >= 2011, detail
	local predchg_p1 = r(p1)
	local predchg_p5 = r(p5)
	local predchg_p10 = r(p10)

	local predchg_p99 = r(p99)
	local predchg_p95 = r(p95)
	local predchg_p90 = r(p90)

// binscatter for actual vs. predicted change in inpatient payments
	cap drop yvar xvar
	cap gen yvar = actchg_tot_pmt_amt_from2010
	cap gen xvar = predchg_tot_pmt_amt_from2010

	sum yvar if fyear >= 2011 & actchg_tot_pmt_amt_from2010 <= `actchg_p99', detail
	local total_y = r(sum)/1e9
	local total_y: display %04.2f `total_y'

	sum xvar if fyear >= 2011 &  actchg_tot_pmt_amt_from2010 <= `actchg_p99', detail
	local total_x = r(sum)/1e9
	local total_x: display %04.2f `total_x'


	reg yvar xvar if fyear >= 2011  &  actchg_tot_pmt_amt_from2010 <= `actchg_p99'
		local reg_coeff = _b[xvar]
		local reg_coeff: display %04.2f `reg_coeff'
		local reg_R2 = e(r2)
		local reg_R2: display %04.2f `reg_R2'

	*binscatterhist yvar xvar if fyear >= 2011  
	binscatterhist yvar xvar if fyear >= 2011  &  actchg_tot_pmt_amt_from2010 <= `actchg_p99' , ///
		histogram(yvar xvar) line(none) ///
		xtitle("extrapolated change in inpatient revenue from 2010") ytitle("actual change in inpatient revenue from 2010")

	graph export "${OutputPath}/Extrapolation/binscatter_tot_pmt_amt_from2010_p1.pdf", replace


// binscatter for actual vs. predicted change in demanded payments
	cap drop yvar xvar
	cap gen yvar = DemandAmt_3y_lawsuit
	cap gen xvar = pred_demand_amt

	sum yvar if fyear >= 2011  & actchg_tot_pmt_amt_from2010 <= `actchg_p99', detail
	local total_y = r(sum)/1e9
	local total_y: display %04.2f `total_y'

	sum xvar if fyear >= 2011  & actchg_tot_pmt_amt_from2010 <= `actchg_p99', detail
	local total_x = r(sum)/1e9
	local total_x: display %04.2f `total_x'


	reg yvar xvar if fyear >= 2011   & actchg_tot_pmt_amt_from2010 <= `actchg_p99'
		local reg_coeff = _b[xvar]
		local reg_coeff: display %04.2f `reg_coeff'
		local reg_R2 = e(r2)
		local reg_R2: display %04.2f `reg_R2'

	*binscatterhist yvar xvar if fyear >= 2011  
	binscatterhist yvar xvar if fyear >= 2011  & actchg_tot_pmt_amt_from2010 <= `actchg_p99' , ///
		histogram(yvar xvar) line(none) ///
		xtitle("extrapolated audit demands") ytitle("actual audit demands")

	graph export "${OutputPath}/Extrapolation/binscatter_demand_amt_lawsuit_from2010_p1.pdf", replace


log close
